/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thinkinginjava.svn.generics.coffee;

import java.util.Iterator;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import thinkinginjava.svn.generics.Generator;

/**
 *
 * @author public_enemy
 */
public class CoffeGenerator implements Generator<Coffe>, Iterable<Coffe> {

    private class CofeeIterator implements Iterator<Coffe> {

        int count = size;

        public CofeeIterator() {
        }

        @Override
        public boolean hasNext() {
            return count > 0;
        }

        @Override
        public Coffe next() {
            count--;
            return CoffeGenerator.this.next();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    private Class[] types = {Latte.class, Mocha.class, Cappuccino.class, Aericano.class, Breve.class};
    private static Random rand = new Random(47);

    public CoffeGenerator() {
    }
    private int size = 0;

    public CoffeGenerator(int sz) {
        size = sz;
    }

    /**
     * 
     * @return
     */
    @Override
    public Coffe next() {
        try {
            return (Coffe) types[rand.nextInt(types.length)].newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(CoffeGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CoffeGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(CoffeGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public Iterator<Coffe> iterator() {
        return new CofeeIterator();
    }

    public static void main(String[] args) {
        CoffeGenerator gen = new CoffeGenerator();
        for (int i = 0; i < 5; i++) {
            System.out.println(gen.next());

        }

        for (Coffe c : new CoffeGenerator(5)) {
            System.out.println(c);
        }
    }
}
